/*
 * SDL控制
 * 
 * 最后修改: 2006年12月1日星期五
 * 文曦畅
 *
 * 历史:
 */

#include <SDL.h>
#include <stdlib.h>

#include "base.h"

static SDL_Event event;

int ctrl_init()
{
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
			    SDL_DEFAULT_REPEAT_INTERVAL);
	SDL_EnableUNICODE(1);
	return 0;
}

int ctrl_do_event()	//得到是否有事件发生
{
	int ret = SDL_PollEvent(&event);
	if(ret && event.type == SDL_QUIT) exit(0);
	if(ret) return event.type;
	return 0;
}
/*
int ctrl_get_event_type() 
{	//得到发生事件的类型
	switch(event.type) {
		case SDL_KEYDOWN:
			return 1;
		case SDL_KEYUP:
			return 2;
		case SDL_MOUSEBUTTONDOWN:
			return 3;
		case SDL_MOUSEBUTTONUP:
			return 4;
		case SDL_MOUSEMOTION:
			return 5;
		case SDL_QUIT:
			exit(0);
		default:
			return 0;
	}
	return 0;
}
*/
int ctrl_get_key()		//得到事件发生的键
{
	return event.key.keysym.sym;
}

Uint16 ctrl_get_unicode()		//得到事件发生的字母
{
	return event.key.keysym.unicode;
}

int ctrl_get_mouse(int *x, int *y)
{
	*x = event.button.x;
	*y = event.button.y;
	return 0;
}

int ctrl_get_botton()
{
	return event.button.button;
}



struct Timer *ctrl_create_timer()
{
	struct Timer *tmp = malloc(sizeof(struct Timer));
	tmp -> enabled = 0;
	tmp -> interval = 0;
	tmp -> lasttime = SDL_GetTicks();
	return tmp;
}

int ctrl_set_timer(struct Timer *t, unsigned int intv, int en)
{
	t -> enabled = en;
	t -> interval = intv;
	t -> lasttime = SDL_GetTicks();
	return 0;
}

int ctrl_test_timer(struct Timer *t)
{
	unsigned int tmp, dt;
	if (!(t -> enabled)) return 0;
	tmp = SDL_GetTicks();
	dt = tmp - (t -> lasttime);
	if(dt >= t -> interval) {
		t -> lasttime = tmp;
		return 1;
	}
	return 0;
}

void ctrl_destory_timer(struct Timer *t)
{
	free(t);
}

